import { IParam, IUIService } from '../interface';

export class AuthUtil {
	/**
	 * 计算界面行为权限状态
	 *
	 * @export
	 * @param {IParam} data 数据
	 * @param {IParam} ActionModel 行为模型
	 * @param {*} UIService UI服务
	 * @return {*}
	 */
	static calcActionItemAuthState(
		data: IParam,
		ActionModel: IParam,
		UIService: any
	) {
		let result: number[] = [];
		if (UIService) {
			for (const key in ActionModel) {
				if (ActionModel.hasOwnProperty(key)) {
					const item = ActionModel[key];
					let dataActionResult: any;
					if (item && item['dataAccessAction']) {
						if (
							Object.is(item['actionTarget'], 'NONE') ||
							Object.is(item['actionTarget'], '')
						) {
							dataActionResult = UIService.getAllOPPrivs(
								undefined,
								item['dataAccessAction']
							);
						} else {
							if (data && Object.keys(data).length > 0) {
								dataActionResult = UIService.getAllOPPrivs(
									data,
									item['dataAccessAction']
								);
							}
						}
					} else {
						dataActionResult = 1;
					}
					// 无权限:0;有权限:1
					if (dataActionResult === 0) {
						// 禁用:1;隐藏:2;隐藏且默认隐藏:6
						if (item.noPrivDisplayMode === 1) {
							item.disabled = true;
						}
						if (item.noPrivDisplayMode === 2 || item.noPrivDisplayMode === 6) {
							item.visible = false;
						} else {
							item.visible = item.visible && true;
						}
					}
					if (dataActionResult === 1) {
						item.visible = item.visible && true;
						item.disabled = item.disabled || false;
					}
					// 返回权限验证的结果
					item.dataActionResult = dataActionResult;
					result.push(dataActionResult);
				}
			}
		}
		return result;
	}

	/**
	 * 计算工具栏项状态
	 *
	 * @export
	 * @param {boolean} state 状态值
	 * @param {IParam} toolbarCtrls 工具栏部件集合
	 */
	static calcToolbarItemState(state: boolean, toolbarCtrls: IParam) {
		const computeItemState = (models: any) => {
			for (const key in models) {
				if (models.hasOwnProperty(key)) {
					const item = models[key];
					if (
						item.uiAction &&
						(Object.is(item.uiAction.actionTarget, 'SINGLEKEY') ||
							Object.is(item.uiAction.actionTarget, 'MULTIKEY') ||
							Object.is(item.uiAction.actionTarget, 'SINGLEDATA'))
					) {
						item.disabled = state;
					}
					item.visible = true;
					if (item.noPrivDisplayMode && item.noPrivDisplayMode === 6) {
						item.visible = !state;
					}
					if (item.itemType === 'ITEMS' && item.items.length > 0) {
						computeItemState(item.items);
					}
				}
			}
		};
		if (toolbarCtrls) {
			Object.keys(toolbarCtrls).forEach((name: string) => {
				computeItemState(toolbarCtrls[name]);
			});
		}
	}

	/**
	 * 计算工具栏权限状态
	 *
	 * @export
	 * @param {IParam} data 数据
	 * @param {IParam} toolbarCtrls 工具栏部件集合
	 * @param {*} UIService UI服务
	 * @return {*}
	 */
	static calcToolbarItemAuthState(
		data: IParam,
		toolbarCtrls: IParam,
		UIService: IUIService
	) {
		const computeAuthState = (models: any) => {
			for (const key in models) {
				if (!models.hasOwnProperty(key)) {
					const item = models[key];
					if (item && item['dataAccessAction']) {
						let dataActionResult: any;
						// 不需要数据的界面行为
						if (
							item.uiAction &&
							(Object.is(item.uiAction.actionTarget, 'NONE') ||
								!item.uiAction.actionTarget)
						) {
							if (
								!item.uiAction.actionTarget &&
								Object.is(item.uiAction.uIActionTag, 'Save') &&
								UIService.isEnableDEMainState
							) {
								if (data && Object.keys(data).length > 0) {
									dataActionResult = UIService.getOPPrivsAuth(
										item['dataAccessAction'],
										data
									);
								}
							} else {
								dataActionResult = UIService.getOPPrivsAuth(
									item['dataAccessAction'],
									{}
								);
							}
						} else {
							if (
								data &&
								Object.keys(data).length > 0 &&
								UIService.isEnableDEMainState
							) {
								dataActionResult = UIService.getOPPrivsAuth(
									item['dataAccessAction'],
									data
								);
							} else {
								dataActionResult = UIService.getOPPrivsAuth(
									item['dataAccessAction'],
									{}
								);
							}
						}
						// 无权限:0;有权限:1
						if (dataActionResult === 0) {
							// 禁用:1;隐藏:2;隐藏且默认隐藏:6
							if (item.noPrivDisplayMode === 1) {
								models[key].disabled = true;
							} else {
								models[key].visible = false;
							}
						}
						if (dataActionResult === 1) {
							models[key].visible = true;
							models[key].disabled = models[key].disabled || false;
						}
					}
					if (item.itemType === 'ITEMS' && item.items.length > 0) {
						computeAuthState(item.items);
					}
				}
			}
		};
		if (UIService && toolbarCtrls) {
			Object.keys(toolbarCtrls).forEach((name: string) => {
				computeAuthState(toolbarCtrls[name]);
			});
		}
	}
}
